package com.platform.usercenter.controller;

import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.security.annotation.RequiresPermissions;
import com.platform.usercenter.domain.vo.UserVo;
import com.platform.usercenter.service.RokidService;
import com.platform.usercenter.utils.ListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/rokid")
public class RokidController extends BaseController {
    @Resource
    RokidService rokidService;

    @RequiresPermissions("system:user:registRa")
    @PostMapping("/registerBatchUsers")
    public AjaxResult registerBatchUsers(@RequestBody UserVo userVo) {
        if (StringUtils.isEmpty(userVo.getUserids())) {
            return AjaxResult.error("参数缺失");
        }
        try {
            List<Long> listU = ListUtil.convey(ListUtil.split(userVo.getUserids(), ","), Long.class);
            Long[] userIds = listU.stream().toArray(Long[]::new);
            rokidService.registerBatchUsers(userIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("初始化用户到远程协作成功");
    }

    @PostMapping("/updateUsersToRokid")
    public AjaxResult updateUsersToRokid(@RequestBody UserVo userVo) {
        if (StringUtils.isEmpty(userVo.getUserids())) {
            return AjaxResult.error("参数缺失");
        }
        try {
            List<Long> listU = ListUtil.convey(ListUtil.split(userVo.getUserids(), ","), Long.class);
            Long[] userIds = listU.stream().toArray(Long[]::new);
            rokidService.updateUsersToRokid(userIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改用户到远程协作异常:" + e.getMessage());
        }
        return AjaxResult.success("修改用户到远程协作成功");
    }

    @RequiresPermissions("system:user:deleteRa")
    @PostMapping("/deleteRokidUsers")
    public AjaxResult deleteRokidUsers(@RequestBody UserVo userVo) {
        if (StringUtils.isEmpty(userVo.getUserids())) {
            return AjaxResult.error("参数缺失");
        }
        try {
            List<Long> listU = ListUtil.convey(ListUtil.split(userVo.getUserids(), ","), Long.class);
            Long[] userIds = listU.stream().toArray(Long[]::new);
            rokidService.deleteRokidUsers(userIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("删除远程协作用户成功");
    }

    /**
     * 全量初始化远程协作用户
     *
     * @return
     */
    @RequestMapping("/initRokidUsers")
    public AjaxResult initRokidUsers() {
        try {
            rokidService.registerBatchUsers(null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("初始化用户到远程协助成功");
    }
}
